package com.ctbri.JWLetter.controller;

import com.alibaba.fastjson.JSONObject;
import com.ctbri.JWLetter.frame.JWJSONObject;
import com.ctbri.JWLetter.frame.RequestTemplate;
import com.ctbri.JWLetter.frame.ResponseTemplate;
import com.ctbri.JWLetter.service.LetterService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 信件控制层实现
 * 
 * @author Hogan
 * 
 */
@Controller
@RequestMapping("/letter")
public class LetterController {

	private Logger log = Logger.getLogger(getClass());

	@Resource
	private LetterService letterService;

	/**
	 * 加载所有举报信
	 * 
	 * @param jo
	 * @return
	 */
	@RequestMapping("loadAllLetters")
	public @ResponseBody JSONObject loadAllLetters(@RequestBody JSONObject jo) {
		JWJSONObject detail = null;
		try {
			RequestTemplate rt = new RequestTemplate(jo);
			detail = letterService.loadAllLetters(rt.getJParams());
		} catch (Exception e) {
			log.error("加载所有举报信失败!", e);
			return new ResponseTemplate().getReturn();
		}
		return new ResponseTemplate(detail).getReturn();
	}
	
	/**
	 * 加载所有标签
	 * 
	 * @param jo
	 * @return
	 */
	@RequestMapping("loadTags")
	public @ResponseBody JSONObject loadTags(@RequestBody JSONObject jo) {
		JWJSONObject detail = null;
		try {
			RequestTemplate rt = new RequestTemplate(jo);
			detail = letterService.loadTags(rt.getJParams());
		} catch (Exception e) {
			log.error("加载所有标签失败!", e);
			return new ResponseTemplate().getReturn();
		}
		return new ResponseTemplate(detail).getReturn();
	}
	
	/**
	 * 加载各种信件记录数
	 * 
	 * @param jo
	 * @return
	 */
	@RequestMapping("loadLetterCounts")
	public @ResponseBody JSONObject loadLetterCounts(@RequestBody JSONObject jo) {
		JWJSONObject detail = null;
		try {
			RequestTemplate rt = new RequestTemplate(jo);
			detail = letterService.loadLetterCounts(rt.getJParams());
		} catch (Exception e) {
			log.error("加载各种信件记录数失败!", e);
			return new ResponseTemplate().getReturn();
		}
		return new ResponseTemplate(detail).getReturn();
	}

	/**
	 * 加载未读信件
	 *
	 * @param jo
	 * @return
	 */
	@RequestMapping("loadUndoLetters")
	public @ResponseBody JSONObject loadUndoLetters(@RequestBody JSONObject jo) {
		JWJSONObject detail = null;
		try {
			RequestTemplate rt = new RequestTemplate(jo);
			detail = letterService.loadUndoLetters(rt.getJParams());
		} catch (Exception e) {
			log.error("加载未读信件失败!", e);
			return new ResponseTemplate().getReturn();
		}
		return new ResponseTemplate(detail).getReturn();
	}

	/**
	 * 加载等级信件
	 * 
	 * @param jo
	 * @return
	 */
	@RequestMapping("loadLevelLetters")
	public @ResponseBody JSONObject loadLevelLetters(@RequestBody JSONObject jo) {
		JWJSONObject detail = null;
		try {
			RequestTemplate rt = new RequestTemplate(jo);
			detail = letterService.loadLevelLetters(rt.getJParams());
		} catch (Exception e) {
			log.error("加载等级信件失败!", e);
			return new ResponseTemplate().getReturn();
		}
		return new ResponseTemplate(detail).getReturn();
	}

	/**
	 * 加载标签信件
	 * 
	 * @param jo
	 * @return
	 */
	@RequestMapping("loadTagLetters")
	public @ResponseBody JSONObject loadTagLetters(@RequestBody JSONObject jo) {
		JWJSONObject detail = null;
		try {
			RequestTemplate rt = new RequestTemplate(jo);
			detail = letterService.loadTagLetters(rt.getJParams());
		} catch (Exception e) {
			log.error("加载标签信件失败!", e);
			return new ResponseTemplate().getReturn();
		}
		return new ResponseTemplate(detail).getReturn();
	}
	
	/**
	 * 加载信件详情
	 * 
	 * @param jo
	 * @return
	 */
	@RequestMapping("loadLetterDetail")
	public @ResponseBody JSONObject loadLetterDetail(@RequestBody JSONObject jo) {
		JWJSONObject detail = null;
		try {
			RequestTemplate rt = new RequestTemplate(jo);
			detail = letterService.loadLetterDetail(rt.getJParams());
		} catch (Exception e) {
			log.error("加载信件详情失败!", e);
			return new ResponseTemplate().getReturn();
		}
		return new ResponseTemplate(detail).getReturn();
	}

}
